from sqlalchemy import TIMESTAMP, Column, Enum, Integer, String, Text, func
from database import Base

class Task(Base):
    __tablename__ = 'tasks'
    
    id = Column(String(64), primary_key=True)
    status = Column(String(20), nullable=False)  # pending, processing, completed, failed
    question = Column(Text)
    file_path = Column(Text)
    result = Column(Text)
    created_at = Column(Integer)
    updated_at = Column(Integer)


class SysUser(Base):
    __tablename__ = 'sys_user'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(50), unique=True, nullable=False)
    password = Column(String(255), nullable=False)
    email = Column(String(100), unique=True)
    phone_number = Column(String(15), unique=True)  # 手机号字段，长度15位
    created_at = Column(Integer)


class InviteCode(Base):
    __tablename__ = 'invite_codes'
    
    code = Column(String(20), primary_key=True)
    is_used = Column(Integer, default=0)
    created_at = Column(Integer)


class PromptHistory(Base):
    __tablename__ = 'prompt_history'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    prompt_text = Column(Text, nullable=False)
    version = Column(Integer, nullable=False)
    created_by = Column(String(50), nullable=False)
    created_at = Column(Integer)


class LoginSession(Base):
    __tablename__ = 'login_sessions'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, nullable=False)
    token = Column(String(255), nullable=False)
    created_at = Column(Integer)
    expires_at = Column(Integer)
    is_active = Column(Integer, default=1)

class Conversation(Base):
    __tablename__ = 'conversations'

    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(255), nullable=False)
    creator_id = Column(Integer, nullable=False)
    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())


class Message(Base):
    __tablename__ = 'messages'

    id = Column(Integer, primary_key=True, autoincrement=True)
    conversation_id = Column(Integer, nullable=False)
    user_id = Column(Integer, nullable=False)
    role = Column(Enum('user', 'assistant', 'thinking'), nullable=False)
    content = Column(Text, nullable=False)
    created_at = Column(TIMESTAMP, server_default=func.now())
